/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/24 17:10:51
 */
Ext.define('Monkey.view.permission.GrantPermissionController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-grantpermission',

    setMenuPermission: function (menuId) {
        var view = this.getView();
        var itemselector = this.getReferences().permissionCmp.down('itemselector');
        var moduleValue = view.moduleValue;
        var moduleParam = view.moduleParam;
        var getMenuPermissionURL = view.getMenuPermissionURL;
        this.request(getMenuPermissionURL + '?' + moduleParam + '=' + moduleValue,
            'GET',
            {
                menuId: menuId,
                fields: 'id,name'
            },
            function (result) {
                var values = [];
                for (var i = 0; i < result.data.length; i++) {
                    values.push(result.data[i].id);
                }
                var oldValues = itemselector.getValue();
                if (!itemselector.isEqual(values, oldValues)) {
                    view.changeTree = true;
                    itemselector.setValue(values);
                }
            });
    },

    selectResource: function (cmp, record) {
        var permissionCmp = this.getReferences().permissionCmp;
        permissionCmp.setTitle('分配权限：<span style="color:red">' + record.data.name + '</span>');
        this.setMenuPermission(record.data.id);
        var view = this.getView();

        var menuTreePanel = this.getReferences().menuTreePanel;
        menuTreePanel.getStore().each(function (r) {
            r.set("checked", false);
        });
        record.set("checked", true);
    },

    saveMenuPermission: function (cmp, newValue, oldValue) {
        var me = this;
        var view = me.getView();
        if (view.changeTree) {
            view.changeTree = false;
            return;
        }
        var method = '';
        var permissionIds;
        if (newValue.length > oldValue.length) {
            method = 'POST';
            permissionIds = Ext.Array.difference(newValue, oldValue);
        } else {
            method = 'PUT';
            permissionIds = Ext.Array.difference(oldValue, newValue)
        }
        if (Ext.isEmpty(permissionIds)) {
            return;
        }
        var moduleValue = view.moduleValue;
        var moduleParam = view.moduleParam;
        var menuTreePanel = this.getReferences().menuTreePanel;
        var records = menuTreePanel.getChecked();
        if (records.length == 0) {
            var permissionCmp = me.getReferences().permissionCmp;
            permissionCmp.setTitle('分配权限：<span style="color:red">请先选中菜单资源</span>');
            var itemselector = permissionCmp.down('itemselector');
            itemselector.setValue('');
            return;
        }
        var menuId = records[0].data.id;
        me.request(view.grantRecoveryPermissionURL + '?' + moduleParam + '=' + moduleValue, method, {
            menuId: menuId,
            permissionIds: Monkey.Utils.arrayToString(permissionIds, ',')
        }, function (result) {
            if (!result.success) {
                Ext.Msg.show({
                    title: '提示',
                    message: result.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.Msg.ERROR
                });
                me.setMenuPermission(menuId);
            }
        });
    }

});
